package com.example.jpa.controller;

import com.example.jpa.service.RougeService;
import com.example.jpa.service.dto.BrandInfo;
import com.example.jpa.service.dto.RougeInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : zhayh
 * @date : 2021-3-31 20:12
 * @description :
 */

@RestController
@RequestMapping("/api/jpa")
@Api(tags = "口红商品管理 - JPA")
public class RougeJpaController {
    @Resource
    private RougeService rougeJpaService;

    @ApiOperation(value = "获取全部品牌")
    @GetMapping("/brands")
    public ResponseEntity<List<BrandInfo>> getAllBrand() {
        List<BrandInfo> result = rougeJpaService.getAllBrandInfo();
        return ResponseEntity.ok(result);
    }

    @ApiOperation(value = "添加商品数据")
    @ApiImplicitParam(name = "rougeInfo", value = "口红对象", dataTypeClass = RougeInfo.class)
    @PostMapping("/rouge")
    public ResponseEntity addRouge(@RequestBody RougeInfo rougeInfo) {
        if (rougeInfo == null || rougeInfo.getId() != null) {
            return ResponseEntity.badRequest().body("商品对象格式不正确");
        }
        RougeInfo rouge = rougeJpaService.addRouge(rougeInfo);
        return ResponseEntity.ok(rouge);
    }

    @ApiOperation(value = "更新口红价格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "price", value = "商品价格", dataTypeClass = Float.class)
    })
    @PutMapping("/rouge")
    public ResponseEntity updateRougePrice(Long id, Float price) {
        if (id == null || id == 0) {
            return ResponseEntity.badRequest().body("商品编码错误");
        } else if (price == null || price <= 0) {
            return ResponseEntity.badRequest().body("商品价格信息错误");
        }
        RougeInfo rouge = rougeJpaService.updateRougePrice(id, price);
        return ResponseEntity.ok(rouge);
    }

    @ApiOperation(value = "删除口红数据")
    @ApiImplicitParam(name = "id", value = "商品id", dataTypeClass = Long.class)
    @DeleteMapping("/rouge/{id}")
    public ResponseEntity<String> deleteRouge(@PathVariable Long id) {
        if (id == null || id == 0) {
            return ResponseEntity.badRequest().body("商品编码错误");
        }
        if (rougeJpaService.deleteRouge(id)) {
            return ResponseEntity.ok("删除成功");
        } else {
            return ResponseEntity.badRequest().body("删除错误");
        }
    }

    @ApiOperation("查询一支口红数据")
    @GetMapping("/rouges/{id}")
    public ResponseEntity getRougeById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return ResponseEntity.badRequest().body("商品编码错误");
        }
        RougeInfo result = rougeJpaService.getRougeById(id);
        return ResponseEntity.ok(result);
    }

    @ApiOperation("根据关键字查询口红的分页数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "查询关键字"),
            @ApiImplicitParam(name = "pageIndex", value = "起始页码", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageSize", value = "每页数据", dataTypeClass = Integer.class)
    })
    @GetMapping("/rouges/keyword")
    public ResponseEntity<Page<RougeInfo>> getRougeByKeyword(@RequestParam String keyword,
                                                             @RequestParam Integer pageIndex,
                                                             @RequestParam Integer pageSize) {
        Page<RougeInfo> result = rougeJpaService.getRougeInfoByKeyword(keyword, pageIndex, pageSize);
        return ResponseEntity.ok(result);
    }
}
